package com.car.util;

import com.car.exception.CustomException;
import com.car.token.DetectionToken;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

/**
 * @author 党怡承
 * @since 2021-08-09
 * @description 车牌结果集工具类
 */
@Component
public class LicensePlate {

  public static String licensePlate(String imgPath) {
    try {
      String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
      byte[] image = FileUtil.readFileByBytes(imgPath);
     String imageUrl = Base64Util.encode(image);
     String imgParam = URLEncoder.encode(imageUrl,"UTF-8");
     String param = "image=" + imgParam;
      // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
      String accessToken = DetectionToken.getToken();
      String result = HttpUtil.post(url, accessToken,param);
      return result;
    } catch (Exception e) {
      throw new CustomException(20001,
          "The current license plate recognition result set is empty, please select another image");
    }
  }
}
